package com.dashan.backend.datastore;

import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class DsSession {
    public enum Status {
         INITIAL("initial"),
         ACTIVE("active"),
         EXPIRED("expired");

         private static final Map<String, Status> lookup = new HashMap<String, Status>();

         static {
              for(Status s : EnumSet.allOf(Status.class))
                   lookup.put(s.getName(), s);
         }

         private String name;

         private Status(String name) { this.name = name;}

         public String getName() { return name; }

         public static Status get(String name) { return lookup.get(name);}
    }
    
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    @Persistent
    private String userName;

    @Persistent
    private String salt;
    
    @Persistent
    private Date timestamp;
    
    @Persistent
    private String statusName;

    public DsSession(String userName, String salt, Date timestamp) {
        this.userName = userName;
        this.salt = salt;
        this.timestamp = timestamp;
        this.statusName = Status.INITIAL.getName();
    }
    
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSalt() {
        return salt;
    }

    public Long getId() {
        return id;
    }
    
    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public Status getStatus() {
        return Status.get(statusName);
    }

    public void setStatus(Status status) {
        this.statusName = status.getName();
    }
    
    @Override
    public String toString() {
        return id + ":" + userName + ":" + statusName;
    }
}
